let gulp = require('gulp'),
    notify = require('gulp-notify'), //报错与不中断当前任务
    less = require('gulp-less'),//编译less
    plumber = require('gulp-plumber'),  //处理管道崩溃问题
    sequence = require("gulp-sequence"),//顺序执行
    rename = require("gulp-rename"),
    minImage = require("gulp-imagemin"),//图片压缩
    px2rpx = require('gulp-px2rpx'),
    minImageForPng = require("imagemin-pngquant");//图片压缩（png）
//压缩图片
gulp.task('minImage', function () {
    return gulp.src('src/images/**/*.{png,jpg,gif,ico}')
        .pipe(minImage({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],
            use: [minImageForPng()]
        }))
        .pipe(gulp.dest('dist/images'));
});

let pxSetting = {
    screenWidth: 1, // 设计稿屏幕, 默认750
    wxappScreenWidth: 1, // 微信小程序屏幕, 默认750
    remPrecision: 2 // 小数精度, 默认6
}
//编译less
gulp.task('less', function () {
    return gulp.src('src/pages/**/*.less')  //找到需要编译的less文件
        .pipe(plumber({errorHandler: notify.onError('Error:<%= error.message %>;')}))
        .pipe(less())  //如果没错误，就编译less
        // .pipe(px2rpx(pxSetting))
        .pipe(rename({extname: '.wxss'}))
        .pipe(gulp.dest('dist/pages'))
});
// gulp.task('less-app', function () {
//     return gulp.src('src/app.less')  //找到需要编译的less文件
//         .pipe(plumber({errorHandler: notify.onError('Error:<%= error.message %>;')}))
//         .pipe(less())  //如果没错误，就编译less
//         .pipe(px2rpx(pxSetting))
//         .pipe(rename({extname: '.wxss'}))
//         .pipe(gulp.dest('dist'))
// });
gulp.task("default", ['less'], function () {
    gulp.watch('src/**/*.less', ['less']);
    gulp.watch('src/app.less', ['less-app']);
});
